package sveta.applogic1;

import java.util.NoSuchElementException;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.WebDriverWait;

import sveta.pages.PageManager;

public class DriverBasedHelper {
	protected WebDriver driver;
	protected WebDriverWait wait;
	protected PageManager pages;
	private boolean acceptNextAlert = true;
	
	public DriverBasedHelper(WebDriver driver){
		this.driver = driver;
	   	wait = new WebDriverWait(driver, 10);
	   	pages = new PageManager(driver);
	}
	public boolean isElementPresent(By by) {
	    try {
	      driver.findElement(by);
	      return true;
	    } catch (NoSuchElementException e) {
	      return false;
	    }
	  }
	
     public String closeAlertAndGetItsText() {
		
		try {
		      Alert alert = driver.switchTo().alert();
		      String alertText = alert.getText();
		      if (acceptNextAlert) {
		        alert.accept();
		      } else {
		        alert.dismiss();
		      }
		      return alertText;
		    } finally {
		      acceptNextAlert = true;
		    }
		  }
     
}
